<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">区域名称</label>
        <el-input v-model="query.name" clearable placeholder="区域名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <span slot="left">
          <el-button
            v-if="crud.optShow.edit"
            v-permission="permission.edit"
            class="filter-item"
            size="mini"
            type="success"
            icon="el-icon-tickets"
            @click="showMapArea()"
          >标注展示</el-button>
        </span>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="130px">
          <el-form-item label="区域名称">
            <el-input v-model="form.name" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="宽度">
            <el-input v-model="form.lineWidth" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="颜色类型">
            <el-select v-model="form.color" style="width: 250px;" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.area_color"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="图案类型">
            <el-select v-model="form.remark" style="width: 250px;" filterable placeholder="请选择">
              <el-option
                v-for="item in lineType"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属矿图">
            <el-select v-model="form.mineMapId" style="width: 250px;" placeholder="请选择">
              <el-option v-for="item in mapList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="坐标">
            <el-input v-model="form.point" :readonly="true" style="width: 250px;">
              <el-button slot="prepend" icon="el-icon-search" @click.native="getAreapoint()" />
            </el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="区域名称" />
        <el-table-column prop="color" label="颜色类型">
          <template slot-scope="scope">
            {{ dict.label.area_color[scope.row.color] }}
          </template>
        </el-table-column>
        <el-table-column prop="mineMapId" label="所属矿图">
          <template slot-scope="scope">
            {{ getMapName(scope.row.mineMapId) }}
          </template>
        </el-table-column>
        <el-table-column prop="point" label="坐标" show-overflow-tooltip />
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-if="checkPer(['admin','mapArea:edit','mapArea:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :append-to-body="true" :visible.sync="mapPointDialog" title="地点定位" top="2vh" width="1250px">
        <div style="height:72vh;width:100%;">
          <MapAreaChart v-if="mapPointDialog" ref="mapPoint" :map-list="mapList" :map-id="form.mineMapId" :is-draw="isDraw" :is-line="isLine" :chart-object="chartObject" @clickCallback="mapCallback" />
        </div>
      </el-dialog>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMapArea from '@/api/mapArea'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getAll } from '@/api/basicMap'
import MapAreaChart from '@/views/components/MapAreaChart'

const defaultForm = { lineWidth: null, id: null, name: null, color: null, mineMapId: null, point: null, remark: null }
export default {
  name: 'MapArea',
  components: { MapAreaChart, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['area_color'],
  cruds() {
    return CRUD({ title: '地图区域', url: 'api/mapArea', idField: 'id', sort: 'id,desc', crudMethod: { ...crudMapArea }})
  },
  data() {
    return {
      isLine: false,
      mapPointDialog: false,
      mapList: [],
      lineType: [{ id: '1', value: 'line', label: '线' }, { id: '2', value: 'polygon', label: '多边形' }],
      permission: {
        add: ['admin', 'mapArea:add'],
        edit: ['admin', 'mapArea:edit'],
        del: ['admin', 'mapArea:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'name', display_name: '区域名称' }
      ],
      isDraw: false
    }
  },
  mounted() {
    if (this.$route.query.value) {
      console.log(this.$route.query.value)
      this.query.name = this.$route.query.value
    } else {
      this.query.name = ''
    }
    this.crud.toQuery()
    this.getAllMap()
  },
  methods: {
    getAllMap() {
      getAll().then(res => {
        this.mapList = res
      })
    },
    getAreapoint() {
      this.isDraw = true
      if (this.form.remark === 'line') {
        this.isLine = true
      } else {
        this.isLine = false
      }
      if (this.form.mineMapId) {
        this.mapPointDialog = true
        this.chartObject = this.form
      } else {
        this.$message({
          message: '请先选择地图，再进行标点。',
          center: true,
          type: 'error'
        })
      }
    },
    // 标点回调，接收所点击的像素坐标
    mapCallback(data) {
      this.form.point = data
      this.mapPointDialog = false
    },
    showMapArea() {
      console.log(this.mapList[0].id)
      this.mapPointDialog = true
      this.chartObject = this.mapList[0]
      this.form.mineMapId = this.mapList[0].id
      this.isDraw = false
    },
    getMapName(data) {
      var name
      this.mapList.forEach(res => {
        if (res.id === data) {
          name = res.name
        }
      })
      return name
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
